package com.jiefutong.zyt.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiefutong.zyt.R;
import com.jiefutong.zyt.utils.PrefsUtil;
import com.just.agentweb.AgentWeb;

import static com.umeng.socialize.utils.ContextUtil.getContext;

/**
 * Created by a2384760 on 2019/3/19.
 */

public class MiddleFragment extends Fragment{
    View view;
    private ImageButton backBtn;
    private TextView titleText;
    private RelativeLayout mRlContent;
    private AgentWeb mAgentWeb;
    private String tokens;
    private boolean isViewPrepared=false;//是否初始化完成

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_middle,null);
        tokens= PrefsUtil.getString(getActivity(), "token","");

        initView();
        loadurl("https://zyt.jiefutong.net/mall/jiefutong/shop/category.html?i=1");
        return view;
    }

    private void initView() {
        titleText = (TextView) view.findViewById(R.id.titleText);

        backBtn = (ImageButton) view.findViewById(R.id.backBtn);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAgentWeb.getWebCreator().getWebView().canGoBack()){
                    mAgentWeb.getWebCreator().getWebView().goBack();
                }else{
                    //finish();
                }


            }
        });
        mRlContent = (RelativeLayout) view.findViewById(R.id.rl_content);

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        // 判断该Fragment时候已经正在前台显示，就可以知道什么时候去加载数据了
       /* if (isVisibleToUser && isViewPrepared) {
            loadurl("https://zhangyitong.shanfuyg.com/mall/jiefutong/shop/category.html?i=1");
        }*/
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void loadurl(String urls) {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mRlContent, new RelativeLayout.LayoutParams(-1, -1))
                .closeIndicator()
                .additionalHttpHeader("Accept", "application/json")
                .additionalHttpHeader("Authorization", tokens)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go(urls);
        String ua = mAgentWeb.getAgentWebSettings().getWebSettings().getUserAgentString();
        mAgentWeb.getAgentWebSettings().getWebSettings().setUserAgentString(ua+" JiefutongClient/"+ getVersionName());

    }

    /**
     * 获取版本号
     *
     * @return 版本号
     */
    public String getVersionName() {
        try {

            PackageManager pm = getContext().getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(getActivity().getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            return super.shouldOverrideUrlLoading(view, url);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }
    };

    private boolean first;

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            titleText.setText(title);
        }
    };
}
